#include "logwidget.h"

LogWidget::LogWidget(QWidget *parent)
{
    this->init_window();
    this->init_control();
    this->init_connect();
}

LogWidget::~LogWidget()
{

}

void LogWidget::init_window()
{
    this->setWindowTitle("操作日志界面");
    this->resize(700,700);
}

void LogWidget::init_control()
{
    this->title = new QLabel("日志操作界面", this);
    this->title->move(230,50);
    this->title->setStyleSheet("font-size:40px;font-weight:bold");
    this->backBtn = new QPushButton("返回", this);
    this->backBtn->move(300, 650);
    this->backBtn->setStyleSheet("width:100px");

    this->tableWidget = new QTableWidget(this);
    this->tableWidget->resize(620,525);
    this->tableWidget->move(40,100);
    this->tableWidget->setFocusPolicy(Qt::NoFocus);

//    this->tableWidget->clearContents();

    //设置列宽
    this->tableWidget->setColumnWidth(0,300);
    this->tableWidget->setColumnWidth(1,300);
    this->tableWidget->setColumnWidth(2,50);
    this->tableWidget->setColumnWidth(3,50);


//    this->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
//    this->tableWidget->setAlternatingRowColors(true);


    this->tableWidget->verticalHeader()->setDefaultSectionSize(50);
    this->tableWidget->verticalHeader()->setVisible(false);

    //行选择模式
    this->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    int i=0,j=0;

    char **qres = nullptr;
    int row=0,col=0;
    int res = LogModel::getInstance()->selectLog(qres, row, col);
    if(res==0)
    {
        if(row>=1)
        {
            //表不为空
            qDebug() << row;
            this->tableWidget->setRowCount(row);
            //设置列数
            this->tableWidget->setColumnCount(4);
        }
        else {
            //表为空
            this->tableWidget->setRowCount(1);
            //设置列数
            this->tableWidget->setColumnCount(4);
        }
    }
    QStringList header;
        header << "序号" << "时间" << "用户" << "内容";
        this->tableWidget->setHorizontalHeaderLabels(header);
        this->tableWidget->horizontalHeader();
        this->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);
        this->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    qDebug () << qres[1*col+j];
    if(row>=1)
    {
        for (i=0;i<row;i++) {

            this->tableWidget->setItem(i,j,new QTableWidgetItem(QString(qres[(i+1)*col+j])));
            j++;
            this->tableWidget->setItem(i,j,new QTableWidgetItem(QString(qres[(i+1)*col+j])));
            j++;
            this->tableWidget->setItem(i,j,new QTableWidgetItem(QString(qres[(i+1)*col+j])));
            j++;
            this->tableWidget->setItem(i,j,new QTableWidgetItem(QString(qres[(i+1)*col+j])));
            j=0;
        }

        for(i=0; i<row;i++)
        {
            for (j=0;j<4;j++) {
                this->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignCenter);
            }
        }
    }

    this->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);


}

void LogWidget::init_connect()
{
    connect(this->backBtn, SIGNAL(clicked()), this, SIGNAL(backToMain()));
}
